package com.hanyao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanyao.reggie.bean.Category;
import com.hanyao.reggie.bean.Setmeal;
import com.hanyao.reggie.common.CustomExcept;
import com.hanyao.reggie.common.R;
import com.hanyao.reggie.dto.SetmealDto;
import com.hanyao.reggie.service.CategoryService;
import com.hanyao.reggie.service.SetmealDishService;
import com.hanyao.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 寒钥
 * @version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @CacheEvict(value="setmealCach", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }
    @GetMapping("/page")

    public R<Page> page(int page, int pageSize, String name){
    //获取套餐分页
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        //创建一个SetmealDtod的分页
        Page<SetmealDto> setmealDishPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(name != null,Setmeal::getName,name);
        setmealService.page(setmealPage);
        //将setmeal的分页的除records属性全部复制到setmealDto的分页中
        //获取setmeal的分页属性records，并将records中每条信息加入到setmealDto中，并给setmealDto中加入新的属性categoryName
        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> list = records.stream().map(item->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null){//判断是否有这个分类id，如果有
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDishPage.setRecords(list);
        return R.success(setmealDishPage);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCach",allEntries = true)//删除setmealCach缓存下的所有缓存坏数据
    public R<String> delete(@RequestParam("ids") List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCach",allEntries = true)//删除setmealCach缓存下的所有缓存坏数据
    public R<String> setStatus(@PathVariable Integer status,
                                   @RequestParam List<Long> ids){
        List<Setmeal> setmealList= ids.stream().map(item->{
            Setmeal setmeal = setmealService.getById(item);
            if(setmeal!=null){
                setmeal.setStatus(status);
                return setmeal;
            }
            return null;
        }).collect(Collectors.toList());
        if(setmealList.isEmpty()){
            throw new CustomExcept("不存在的套餐，无法修改");
        }
        setmealService.updateBatchById(setmealList);
        return R.success("修改成功");
    }

    @GetMapping("/{setmealId}")
    public R<SetmealDto> show(@PathVariable Long setmealId){
        SetmealDto setmealDto = setmealService.showWithDish(setmealId);

        return R.success(setmealDto);
    }

    @PutMapping
    @CacheEvict(value = "setmealCach",allEntries = true)//删除setmealCach缓存下的所有缓存坏数据
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCach",key = "#p0.categoryId+'_'+#p0.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        Long categoryId = setmeal.getCategoryId();
        //if(categoryId==null){
        //    return R.error("没有该类型");
        //}
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);
        return R.success(list);
    }
}
